package com.bootcamp.mall.controller.admin;

/**
 * @program: mall
 * @author: Keyu Li
 * @create: 2021-11-11 17:21
 **/

import com.bootcamp.mall.model.Result;
import com.bootcamp.mall.model.bo.order.ChangeOrderBO;
import com.bootcamp.mall.model.bo.order.OrderByPageBO;
import com.bootcamp.mall.model.vo.order.OrderByPageVO;
import com.bootcamp.mall.model.vo.order.ShowOrderVO;
import com.bootcamp.mall.service.order.OrderService;
import com.bootcamp.mall.service.order.OrderServiceImpl;
import com.bootcamp.mall.utils.JSONUtil;
import com.bootcamp.mall.utils.StringUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "OrdersServlet", value = "/api/admin/order/*")
public class OrdersServlet extends HttpServlet {

    Gson gson = new Gson();
    OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getRequestURI().replace("/api/admin/order/", "");
        if ("order".equals(op)) {
            opShowOrder(request, response);
        } else if ("deleteOrder".equals(op)) {
            opDeleteOrder(request, response);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getRequestURI().replace("/api/admin/order/", "");
        if ("ordersByPage".equals(op)) {
            opOrderByPage(request, response);
        } else if ("changeOrder".equals(op)) {
            opChangeOrder(request, response);
        }
    }


    private void opDeleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 获取id判断合法性
        int id;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
            writer.println(gson.toJson(Result.error("前端参数异常！")));
            return;
        }
        Integer code = orderService.deleteOrder(id);
        if (code == 0) {
            writer.println(gson.toJson(Result.ok()));
            return;
        }
        writer.println(gson.toJson(Result.error("服务器错误！")));
    }

    private void opChangeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收字符串并解析为对象四连
        PrintWriter writer = response.getWriter();
        // json解析四联：获取字符串，判断不为空，获取对象，捕获异常判断参数合法
        String jsonContent = JSONUtil.parseContent(request);
        // 合法性
        if (StringUtil.isEmpty(jsonContent)) {
            writer.println(gson.toJson(Result.error("前端异常！")));
            return;
        }
        // jsonContent 解析为 bo
        // json转换为AddGoodsBO对象
        JsonObject asJsonObject = new JsonParser().parse(jsonContent).getAsJsonObject();
        try {
            ChangeOrderBO changeOrderBO = gson.fromJson(asJsonObject, ChangeOrderBO.class);
            // 一般是肯定能够正常返回的
            Integer code = orderService.changeOrder(changeOrderBO);
            if (code == 0) {
                writer.println(gson.toJson(Result.ok()));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            writer.println(gson.toJson(Result.error("请求参数异常！")));
            return;
        }
        writer.println(gson.toJson(Result.error("服务器异常！")));
    }

    private void opShowOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 获取id三连
        int id;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
            writer.println(gson.toJson(Result.error("前端参数异常！")));
            return;
        }
        ShowOrderVO showOrderVO = orderService.showOrder(id);
        // 一定会成功返回的
        writer.println(gson.toJson(Result.ok(showOrderVO)));
    }

    private void opOrderByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收字符串并解析为对象四连
        PrintWriter writer = response.getWriter();
        // json解析四联：获取字符串，判断不为空，获取对象，捕获异常判断参数合法
        String jsonContent = JSONUtil.parseContent(request);
        // 合法性
        if (StringUtil.isEmpty(jsonContent)) {
            writer.println(gson.toJson(Result.error("前端异常！")));
            return;
        }
        // jsonContent 解析为 bo
        // json转换为AddGoodsBO对象
        JsonObject asJsonObject = new JsonParser().parse(jsonContent).getAsJsonObject();
        try {
            OrderByPageBO orderByPageBO = gson.fromJson(asJsonObject, OrderByPageBO.class);
            OrderByPageVO orderByPageVO = orderService.orderByPage(orderByPageBO);
            // 一定可以找到，除非发生异常
            writer.println(gson.toJson(Result.ok(orderByPageVO)));
        } catch (Exception e) {
            e.printStackTrace();
            writer.println(gson.toJson(Result.error("服务器异常！")));
        }
    }
}
